package com.koocloud.electroplatemanage.auth.mapper;


import com.koocloud.electroplatemanage.pojo.Permission;
import com.koocloud.electroplatemanage.pojo.Role;
import com.koocloud.electroplatemanage.pojo.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;


@Mapper
public interface IUserDao {

    @Insert("insert into user(password,username,fullname,job_num,reg_phone,superior_id,department_id) values(#{password},#{userName},#{fullName},#{jobNum},#{regPhone},#{superiorId},#{departmentId})")
    Integer addUser(@Param("password") String password, @Param("userName") String userName, @Param("fullName") String fullName, @Param("jobNum") String jobNum, @Param("regPhone") String regPhone, @Param("superiorId") String superiorId,@Param("departmentId") String departmentId);

    @Update("update user set id=#{id},username=#{userName},job_num=#{jobNum},password=#{password},fullname=#{fullName},reg_phone=#{regPhone},superior_id=#{superiorId},department_id = #{departmentId}  where id=#{id}")
    Integer updateUser(@Param("id") String id, @Param("userName") String userName, @Param("password") String password, @Param("jobNum") String jobNum, @Param("fullName") String fullName, @Param("regPhone") String regPhone, @Param("superiorId") String superiorId,@Param("departmentId") String departmentId);

    @Update("update user set id=#{id},username=#{userName},job_num=#{jobNum},fullname=#{fullName},reg_phone=#{regPhone},superior_id=#{superiorId} ,department_id = #{departmentId} where id=#{id}")
    Integer updateUserWithOutPassword(@Param("id") String id, @Param("userName") String userName, @Param("jobNum") String jobNum, @Param("fullName") String fullName, @Param("regPhone") String regPhone, @Param("superiorId") String superiorId,@Param("departmentId") String departmentId);

    @Select("Select id,username,job_num,fullname,reg_phone,superior_id,enable_fag,department_id  from user where (id like '%${idOrName}%' or username like '%${idOrName}%' or job_num like '%${idOrName}%' or reg_phone like '%${idOrName}%'  or fullname like '%${idOrName}%') ")
    List<User> selectUser(String idOrName);

    @Select("Select * from user where id = #{id}")
    User selectByPk(@Param("id")String id);

    @Select("Select * from user where job_num = #{jobNum}")
    User selectByJobNum(@Param("jobNum")String jobNum);

    @Select("Select job_num from user where fullname = #{fullName}")
    String selectByFullName(@Param("fullName")String fullName);


    @Delete("update user set enable_fag = #{enableFlag} where id = #{id}")
    Integer deleteUser(@Param("id") String id,@Param("enableFlag") int enableFlag);


    @Select("SELECT * FROM user tu left JOIN  user_role  tur  on  tu.id=tur.user_id  left JOIN  role   tr  on  tur.role_id=tr.id    where " + "(case when #{id} is not null and #{id}!= '' then (tu.id like '%${id}%') else (1=1) end ) and " + " (case when #{userName} is not null and #{userName}!= '' then (tu.username like '%${userName}%') else (1=1) end)")
    List<Map<String, Object>> selectRole(@Param("id") String id, @Param("userName") String userName);

    @Select("SELECT tr.role_name FROM user tu left JOIN  user_role  tur  on  tu.id=tur.user_id  left JOIN  role   tr  on  tur.role_id=tr.id    where tu.id=#{id} ")
    List<String> selectRoleList(String id);

    @Select("select role_name from role ")
    List<String> selectAllRoleList();

    @Delete("delete from user_role where user_id=#{id}")
    Integer deleteUserRoleRelation(String id);

    @Insert("insert into user_role(user_id,role_id) values(#{id},#{roleid})")
    Integer insert(@Param("id") String id, @Param("roleid") String roleid);

    @Select("select permission_name from permission")
    List<String> selectAllAuthList();

    @Select("Select count(*) from user where " + "(case when #{id} is not null and #{id}!= '' then (id like '%${id}%') else (1=1) end ) and " + " (case when #{userName} is not null and #{userName}!= '' then (username like '%${userName}%') else (1=1) end)")
    Integer selectUser2(@Param("id") String id, @Param("userName") String userName);

    @Insert("insert into user(username,password,reg_phone,reg_date) values(#{username},#{password},#{regPhone},#{regDate})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
//加入该注解可以保持对象后，查看对象插入id
    Integer insertUser(User user);

    @Select("select id,username,job_num,fullname,password,reg_phone,enable_fag,superior_id from user where username=#{username}")
    User selectUserByToken(String username);

    @Select("select id,username,job_num,fullname,password,reg_phone,enable_fag,superior_id from user where id=#{id}")
    User selectUserById(int id);

    @Update("update user set password=#{md5}  where id=#{id}")
    Integer updateUserPassword(@Param("id") int id, @Param("md5") String md5);

    @Select("select * from user where username=#{username}")
    List<Map<String, Object>> selectUserByName(String userName);

    @Select("SELECT r.id , r.remarks , r.role_name roleName FROM user u INNER JOIN user_role ru ON u.id = ru.user_id\r\n" +
            "	INNER JOIN role r ON ru.role_id = r.id \r\n" +
            "	WHERE u.id = #{id}")
    List<Role> selectRoleListByUserId(int id);


    @Select("SELECT DISTINCT\r\n" +
            "	m.id,\r\n" +
            "	m.permission_name,\r\n" +
            "	m.url,\r\n" +
            "	m.icon,\r\n" +
            "	m.ORDER,\r\n" +
            "	m.parent_id,\r\n" +
            "	m.lvl,\r\n" +
            "	m.type \r\n" +
            "FROM\r\n" +
            "	role r\r\n" +
            "	INNER JOIN role_permission rm ON r.id = rm.role_id \r\n" +
            "	INNER JOIN permission m ON rm.permission_id = m.id \r\n" +
            "WHERE\r\n" +
            "	r.id=#{id} and m.parent_id !=-1")
    List<Permission> selectAuthListById2(String id);


    @Insert("insert into user(username,password,uuid) values(#{userName},#{md5},#{id})")
    Integer addUserInfo(@Param("id") String id, @Param("userName") String userName, @Param("md5") String md5);


    @Select("select * from user where enable_fag=0  order by superior_id,id")
    List<User> selectOrganizationStruct();


    @Select("select * from user where enable_fag=0  order by superior_id,id")
    List<User> selectAllUser();

    @Select("select * from user where enable_fag=0  and username !=#{userName}   order by superior_id,id")
    List<User> selectAllUserExclude(String userName);

    @Select("select id,username,job_num,fullname,reg_phone,enable_fag,superior_id from user where enable_fag=0  and id!=#{id}   order by superior_id,id")
    List<User> getSuperiorLeaderIds(long id);

    @Select("select count(0) from user where job_num = #{jobNum}")
    int getJobNumWhenAdd(String jobNum);

    @Select("select count(0) from user where job_num = #{jobNum} and id NOT IN(#{id})")
    int getJobNumWhenEdit(@Param("id") String id,@Param("jobNum") String jobNum);

    @Select("A.username,\n" + "A.`password`,\n" + "A.fullname,\n" + "A.job_num,\n" + "A.reg_phone,\n" + "B.department_name \n" + "FROM `user` A\n" + "left join `department` B\n" + "on A.department_id = B.department_id\n" + "GROUP BY\n" + "A.id,\n" + "A.username,\n" + "A.`password`,\n" + "A.fullname,\n" + "A.job_num,\n" + "A.reg_phone,\n" + "B.department_name ")
    List<User> getUserOrderByDepartment();
}
